package com.usermgr.biz;

import java.util.List;

import com.usermgr.bean.OfUser;
import com.usermgr.bean.OfUserCriteria;
import com.usermgr.dao.OfUserDAO;
/**
 *@title      	标题：
 *@description 	描述：
 *@author 	  	作者 ：wuxionghao
 *@createTime 	创建时间：2011-12-29下午03:47:29
 *@version    	版本：v1.0
 */
public class UserManagerImpl implements IUserManager {

	
	private OfUserDAO userDAO;
	//验证用户是否存在
	public boolean findUser(OfUser user){
		boolean result = false;
		OfUserCriteria uc = new OfUserCriteria();
		uc.createCriteria().andOfNameEqualTo(user.getOfName())
						   .andOfPasswordEqualTo(user.getOfPassword());
		
		if(userDAO.selectByExample(uc).size() == 1){
			List list = userDAO.selectByExample(uc);
			result = true;
		} 
		return result;
	}
	//用户注册
	public boolean addUser(OfUser user) {
		try{
			if(user.getOfPermission() == null){
				user.setOfPermission("0");
			}
			if(user.getOfStatus() == null){
				user.setOfStatus("0");
			}
			userDAO.insert(user);
		}catch(Exception e){
			return false;
		}
		return true;
	}

	/**
	 * 根据用户名和密码得到用户ID
	 */
	public Long getUserId(String uName,String password){
		OfUserCriteria example = new OfUserCriteria();
		example.createCriteria().andOfNameEqualTo(uName)
								.andOfPasswordEqualTo(password);
		List<OfUser> users = userDAO.selectByExample(example);
		OfUser user = users.get(0);
		Long userId = user.getOfId();
		return userId;
	}
	
	public int deleteUser(long userId) {
		int reslut=-1;
		reslut=userDAO.deleteByPrimaryKey(userId);
		return reslut;
	}
	
	/**
	 * 修改用户信息
	 */
	public int updateUser(Long userId) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	//修改密码
	public boolean updateUserPassword( String newPassword,Long userId) {
		OfUserCriteria example=new OfUserCriteria();
		example.createCriteria().andOfIdEqualTo(userId);
		OfUser user=new OfUser();
		user.setOfPassword(newPassword);
		int reslut=-1;
		reslut=userDAO.updateByExampleSelective(user, example);
		if(reslut>0){
			return true;
		}
		return false;
	}
	
	public boolean chexkUser(String UserName) {
		OfUserCriteria example=new OfUserCriteria();
		example.createCriteria().andOfNameEqualTo(UserName);
		List userName=userDAO.selectByExample(example);
		if(userName.size()>0&&userName!=null){
			return true;
		}
		return false;
	}

	
	public OfUserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(OfUserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public List<OfUser> getAllUser() {
		return userDAO.getAllUser();
	}
	public List<OfUser> findPermission(OfUser OfUser) {
		return userDAO.findPermission(OfUser);
	}
	
	
}
